class UserConfirm < ActiveRecord::Base
  belongs_to :user
  has_many :humble_site, :dependent => :destroy

  validates_presence_of :user
  validates_presence_of :title
  validates_uniqueness_of :title, :scope => :user_id

  before_destroy :check_deletable!
  # attr_protected :user_id, :deletable
  class NotDeletable < StandardError; end

  def self.properties
    UserConfirm.mappings.keys
  end

  def self.attribute_name_for_type_uri(type_uri)
    prop = mappings.detect { |i| i[1].include?(type_uri) }
    prop ? prop[0] : nil
  end

  # Returns the personas attribute for the given SReg name or AX Type URI
  def property(type)
    
    prop = UserConfirm.mappings.detect { |i| i[1].include?(type) }
    prop ? self.send(prop[0]).to_s : nil
  end

  def date_of_birth
    "#{dob_year? ? dob_year : '0000'}-#{dob_month? ? dob_month.to_s.rjust(2, '0') : '00'}-#{dob_day? ? dob_day.to_s.rjust(2, '0') : '00'}"
  end

  def date_of_birth=(dob)
    res = dob.split("-")
    self.dob_year = res[0]
    self.dob_month = res[1]
    self.dob_day = res[2]
    dob
  end
  
  
  # def info
    # {
      # :email => email.to_s,
      # :nickname => nickname.to_s,
      # :fullname => fullname.to_s,
      # :image_default => image_default,
      # :im_qq => im_qq.to_s
    # }
  # end

  protected

  def check_deletable!
    raise NotDeletable unless deletable
  end

  private

  # Mappings for SReg names and AX Type URIs to attributes
  def self.mappings
    $humble_config['attribute_mappings']
  end
end
